package com.zy.explore.design.patterns.responsibilitychain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 抽象的处理器类
 *
 * @author ZY
 * @date 2021/11/02
 */
public abstract class AbstractHandler {

    /**
     * 责任链中的下一个处理程序
     */
    private AbstractHandler nextHandler;

    /**
     * 设置责任链的下一个处理程序
     */
    public void setNextHandler(AbstractHandler nextHandler){
        this.nextHandler = nextHandler;
    }

    /**
     * 获取责任链中的下一个处理程序
     *
     * @return {@link AbstractHandler}
     */
    public AbstractHandler getNextHandler() {
        return nextHandler;
    }

    /**
     * 具体参数拦截逻辑，留给子类去实现
     *
     * @param request  请求
     * @param response 响应
     */
    public void filter(HttpServletRequest request, HttpServletResponse response) {
        doFilter(request, response);
        // 如果责任链中还有节点处理器未做处理，则继续往下调
        if (getNextHandler() != null) {
            getNextHandler().filter(request, response);
        }
    }

    /**
     * 责任链继续向下处理
     *
     * @param request  请求
     * @param response 响应
     */
    public abstract void doFilter(HttpServletRequest request, HttpServletResponse response);

}
